package com.itheima.springbootwebtlias.controller;


import com.itheima.springbootwebtlias.pojo.Emp;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.service.EmpService;
import com.itheima.springbootwebtlias.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {

        //1.接收请求参数
        log.info("登录验证请求参数：" + emp);
        //2.调用业务层方法
        Emp result = empService.checkLogin(emp);
        log.info("返回结果");
        Map map = new HashMap();
        map.put("id" , result.getId());
        map.put("username" , result.getUsername());
        map.put("name" , result.getName());
        //登录成功，办法令牌。
        String jwtToken = JwtUtils.generateJwt(map);
        map.put("token",jwtToken);

        //3.封装返回结果
        return Result.success(map);
    }



}
